package com.example.myloginapplication.slice;

import com.example.myloginapplication.ResourceTable;
import com.example.myloginapplication.provider.TabPageSliderProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

//public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
public class MainAbilitySlice extends AbilitySlice implements Component.FocusChangedListener {
//    // 定义组件对象
//    TextField username = null;
//    TextField password = null;
//    Text forget = null;
//    Button login = null;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

//        // 锁定
//        username = (TextField) findComponentById(ResourceTable.Id_username);
//        password = (TextField) findComponentById(ResourceTable.Id_password);
//        forget = (Text) findComponentById(ResourceTable.Id_forget);
//        login = (Button) findComponentById(ResourceTable.Id_login);
//
//        // 绑定
//        forget.setClickedListener(this);
//        // 登录
//        login.setClickedListener(this);


        // 初始化TabList
        TabList tabList = (TabList)findComponentById(ResourceTable.Id_tab_list);
        String[] tabListTags = {"首页","分类","特别关心","我的"};
        for (String tabListTag : tabListTags) {
            TabList.Tab tab = tabList.new Tab(this);
            tab.setText(tabListTag);
            tabList.addTab(tab);
        }


        // 初始化PageSlider
        List<Integer> layoutFileIds = new ArrayList<>();
        layoutFileIds.add(ResourceTable.Layout_ability_main_index);
        layoutFileIds.add(ResourceTable.Layout_ability_main_category);
        layoutFileIds.add(ResourceTable.Layout_ability_main_shopcart);
        layoutFileIds.add(ResourceTable.Layout_ability_main_user_center);
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setProvider(new TabPageSliderProvider(layoutFileIds,this));


        // TabList 与 PageSlider  滑动相互配合
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int index = tab.getPosition(); // 获取点击菜单的索引
                pageSlider.setCurrentPage(index);// 设置pageSlider的索引与菜单索引一致
                if(index==0){// 首页
                    initIndex(pageSlider);
                }else if(index==1){//分类
                    initCategory(pageSlider);
                }else if(index==2){//特别关心
                    initShopCart(pageSlider);
                }else if(index==3){//我的
                    initUserCenter(pageSlider);
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
            }
            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }
            @Override
            public void onPageSlideStateChanged(int i) {
            }
            @Override
            public void onPageChosen(int i) {
                if(tabList.getSelectedTabIndex() != i){
                    tabList.selectTabAt(i);
                }
            }
        });
        // tabList默认选中,同时加载PageSlider默认
        tabList.selectTabAt(0);
    }

    private void initIndex(PageSlider pageSlider) {
        // 监听首页输入框，点击输入框跳转到搜索栏目
        TextField searchText = (TextField)pageSlider.findComponentById(ResourceTable.Id_index_search_input);
        searchText.setFocusChangedListener(this);

        //渲染数据从后台获取并展示到main.xml中
        Component itemLayout = pageSlider.findComponentById(ResourceTable.Id_layout_item01);
        itemLayout.setClickedListener(component -> {
            Intent intent = new Intent();
            intent.setParam("itemId","888");
            this.present(new DetailAbilitySlice(),intent);
        });
    }

    private void initCategory(PageSlider pageSlider) {
        TextField searchText = (TextField)pageSlider.findComponentById(ResourceTable.Id_category_search_input);
        searchText.setFocusChangedListener(this);
    }

    private void initShopCart(PageSlider pageSlider) {
    }

    private void initUserCenter(PageSlider pageSlider) {
    }

    @Override
    public void onFocusChange(Component component, boolean b) {
        if(b){
            present(new SearchAbilitySlice(),new Intent());
        }
    }


//    @Override
//    public void onActive() {
//        super.onActive();
//    }
//
//    @Override
//    public void onForeground(Intent intent) {
//        super.onForeground(intent);
//    }
//
//    @Override
//    public void onClick(Component component) {
//        // 业务
//        if(component == forget){
//            // 忘记密码业务处理
//        }else if(component == login){
//            // 登录业务处理，比较返回
//            String usernameInput = username.getText();
//            String passwordInput = password.getText();
//
//            // 比较
//            if("".equals(usernameInput)||"".equals(passwordInput)){
//                // 提示为空
//            }else if("right".equals(usernameInput)&&"right".equals(passwordInput)){
//                // 登录成功并跳转
//            }else{
//                // 提示 用户名或者密码错误
//            }
//        }else{
//            // 其他业务
//        }
//    }
}
